﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

public partial class MembershipCard_wechat_web_zc1 : WeiPage
{
    PersonInfoBO personbo = new PersonInfoBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {           
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=" + GetSecret() + "&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = "wx8ecea9aaa3d6162b";
                        Response.Write(b[13].ToString());
                    }
                }
                catch (Exception)
                {

                }
            }
            //yzm();
            Random_String(4);

        }

    }

    void yzm()          //产生随机验证码
    {
        Random rand = new Random();
        int a = ((int)Math.Floor(rand.NextDouble() * (10000 - 1)) + 1);
        yzmLabel.InnerText = a.ToString();
    }

    private void  Random_String(int validate_string_size)
    {
        string vchar = "23456789ABCDEGHKMNPQRSUVWXYZ我人有的和九主产不七为这工要十在地一上八是中国同经以四发了五民搜索就计六算机合肥业学习科技安徽美女东西南北二三";
        string vnum = "";
        System.Random rand = new Random();
        for (int i = 0; i < validate_string_size; i++)
        {
            int t = rand.Next(vchar.Length);
            vnum += vchar[t];
        }

        yzmLabel.InnerText = vnum;
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtNum.Value.Equals(yzmLabel.InnerText))
        {
            string user=(string)Session["FromUserName"];
            PersonInfo person = new PersonInfo();
            //WeiXinRequest wxrequest = new WeiXinRequest();
            person.Phone = txtPhone.Value;
            person.FromUserName = user;
            personbo.Add(person);
            Response.Redirect("~/wenjuan.aspx");

        }
        else {
            Label1.Text = "您输入的验证码不正确";
        }
    }
}